<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\admin\model\Team;
use app\common\manager\assistant\AssistantAccountManager;
use app\common\model\assistant\Assistant;
use app\common\model\Authentication;
use app\common\model\CaptainApplication;
use app\common\model\Level;
use app\common\model\Merchant;
use app\common\model\RelationImage;
use app\common\model\Store;
use app\common\model\StoreEmployee;
use app\common\model\StoreInfo;
use app\common\model\User;
use think\Db;
use think\Exception;

class EmployeeManager extends BaseManager
{

    CONST TYPE_GROUP = 1;
    CONST TYPE_WRITE = 2;
    CONST TYPE_END = 3;

    CONST TYPE_DEFAULT = 4;

    /**
     * 员工录入
     * @param User $user
     * @param array $data
     * @param int $storeId
     * @param bool $trans
     * @return array
     */
    public static function employee(User $user,array $data,int $storeId,bool $trans=true):array
    {
        if ($trans){
            Db::startTrans();
        }
        try {
            $storeEmployeeModel = new StoreEmployee();
            $StoreInfoModel = new StoreInfo();
            $storeInfo = $StoreInfoModel->where('merchant_id', $user->merchant_id)->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find();
            if (!$storeInfo){
                throw new Exception('您当前无操作权限');
            }

            if (!empty($storeInfo['store_id']) && $storeInfo['store_id'] != $storeId){
                if (!$StoreInfoModel->where('merchant_id',$user->merchant_id)->where('store_id',$storeId)->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find()){
                    throw new Exception('您非当前门店管理人员，无操作权限');
                }
            }

            if ($storeEmployeeModel->where('mobile',$data['mobile'])->where('store_id',$storeId)->find()){
                throw new Exception('当前员工已存在，无法添加');
            }



            $userId = 0;
            $userInfo = User::where('mobile',$data['mobile'])->find();
            if(!$userInfo){
                throw new Exception('当前账号不存在，请注册后再添加员工');
            }

            $userId = $userInfo['id'];
            if ($StoreInfoModel->where('store_id',$storeId)->where('user_id',$userId)->find()){
                throw new Exception('当前人员是当前门店管理或店员，请勿重复添加');
            }

            if(!$id = $storeEmployeeModel->insertGetId([
                'store_id' => $storeId,
                'merchant_id'=>$user->merchant_id,
                'user_id'=>$userId,
                'mobile'=>$data['mobile'],
                'real_name'=>$data['real_name'],
                'is_group'=>$data['is_group'],
                'is_write'=>$data['is_write'],
                'create_time'=>time(),
                'update_time'=>time(),
            ])){
                throw new Exception('添加失败，请联系管理员');
            }

            $StoreInfoModel->insertGetId([
                'store_id' => $storeId,
                'merchant_id' => $user->merchant_id,
                'user_id' => $userInfo['id'],
                'is_manager' => StoreInfo::NOT_MANAGER,
                'create_time' => time(),
            ]);
            $userInfo->is_employee = User::IS_STORE_YES;
            $userInfo->is_merchant = User::IS_MERCHANT_YES;
            $userInfo->merchant_id = $user->merchant_id;
            $userInfo->save();

            if ($data['is_assistant'] && $data['level']) {
                $levelInfo = Level::where('vl_id', $data['level'])->find();
                if (!$levelInfo) {
                    throw new Exception('当前等级不存在，请联系管理员');
                }
                $assistantModel = new Assistant();
                if (!$assistantId = $assistantModel->insertGetId([
                    'user_id' => $userId,
                    'type' => Assistant::TYPE_STORE,
                    'store_id' => $storeId,
                    'employee_id' => $id,
                    'nickname' => $userInfo['nickname'] ?? '',
                    'gender' => $userInfo['gender'] ?? 0,
                    'avatar' => $userInfo['avatar'] ?? '',
                    'birthday' => is_null($userInfo['birthday']) ? '' : $userInfo['birthday'],
                    'level' => $data['level'],
                    'review'=>0,
                    'lat'=>$storeInfo['lat'],
                    'lng'=>$storeInfo['long'],
                    'address' => $storeInfo['address'],
                    'reason'=>'门店添加，无需审核',
                    'review_status'=>Assistant::REVIEW_STATUS_PASS,
                    'assistant_price' => $levelInfo['assistant_price'],
                    'time_unit' => $levelInfo['time_unit'],
                    'create_time' => time(),
                    'update_time' => time()

                ])) {
                    throw new Exception('添加助教信息失败，请联系管理员');
                }

                $result = AssistantAccountManager::account($assistantId,$userInfo['id'],false);
                if ($result['code'] != AssistantAccountManager::$successCode){
                    throw new Exception($result['message']);
                }

                $userInfo->is_assistant = User::IS_ASSISTANT_YES;
                $userInfo->save();
            }
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }

    /**
     * 后台系统添加
     * @param array $data
     * @param int $storeId
     * @param bool $trans
     * @return array
     */

    public static function employeeSys(array $data,int $storeId,bool $trans=true):array
    {
        if ($trans){
            Db::startTrans();
        }
        try {
            $storeEmployeeModel = new StoreEmployee();
            $StoreInfoModel = new StoreInfo();
            if ($storeEmployeeModel->where('mobile',$data['mobile'])->where('store_id',$storeId)->find()){
                throw new Exception('当前员工已存在，无法添加');
            }
            $userId = 0;
            $userInfo = User::where('mobile',$data['mobile'])->find();
            if ($userInfo){
                $userId = $userInfo['id'];
                if ($StoreInfoModel->where('store_id',$storeId)->where('user_id',$userId)->find()){
                    throw new Exception('当前人员是当前门店管理或店员，请勿重复添加');
                }
            }
            $storeInfo = Store::where('id',$storeId)->find();
            if (!$storeInfo){
                throw new Exception('当前门店信息不不能在');
            }

            if(!$id = $storeEmployeeModel->insertGetId([
                'store_id' => $storeId,
                'merchant_id'=>$storeInfo->merchant_id,
                'user_id'=>$userId,
                'mobile'=>$data['mobile'],
                'real_name'=>$data['real_name'],
                'is_group'=>$data['is_group'],
                'is_write'=>$data['is_write'],
                'create_time'=>time(),
                'update_time'=>time(),
            ])){
                throw new Exception('添加失败，请联系管理员');
            }

            if ($userInfo){

                $StoreInfoModel->insertGetId([
                    'store_id' => $storeId,
                    'merchant_id' => $storeInfo->merchant_id,
                    'user_id' => $userInfo['id'],
                    'is_manager' => StoreInfo::NOT_MANAGER,
                    'create_time' => time(),
                ]);
                $userInfo->is_employee = User::IS_STORE_YES;
                $userInfo->is_merchant = User::IS_MERCHANT_YES;
                $userInfo->merchant_id = $storeInfo->merchant_id;
                $userInfo->save();
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }
    /**
     * 商户管理修改店员信息
     * @param User $user
     * @param int $id
     * @param array $data
     * @param int $storeId
     * @param bool $trans
     * @return array
     */
    public static function modify(User $user,int $id,array $data,int $storeId,bool $trans=true):array
    {
        if ($trans){
            Db::startTrans();
        }
        try {

            if (!$employeeInfo = StoreEmployee::where('id', $id)->where('delete',StoreEmployee::DELETE_NO)->find()){
                throw new Exception('当前记录不存在，无法修改');
            }

            $storeEmployeeModel = new StoreEmployee();
            $StoreInfoModel = new StoreInfo();
            $storeInfo = $StoreInfoModel->where('merchant_id', $user->merchant_id)->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find();
            if (!$storeInfo){
                throw new Exception('您当前无操作权限');
            }

            if (!empty($storeInfo['store_id']) && $storeInfo['store_id'] != $storeId){
                if (!$StoreInfoModel->where('merchant_id',$user->merchant_id)->where('store_id',$storeId)->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find()){
                    throw new Exception('您非当前门店管理人员，无操作权限');
                }
            }

            if ($storeEmployeeModel->where('mobile',$data['mobile'])->where('store_id',$storeId)->where('id','<>',$id)->find()){
                throw new Exception('当前员工已存在，无法修改');
            }

            $userId = 0;
            $userInfo = User::where('mobile',$data['mobile'])->find();
            if (!$userInfo){
                throw new Exception('当前账号不存在，请注册后再添加员工');
            }
            $userId = $userInfo['id'];
            if(!$storeEmployeeModel->where('id',$id)->where('delete',StoreEmployee::DELETE_NO)->update([
                'user_id'=>$userId,
                'mobile'=>$data['mobile'],
                'real_name'=>$data['real_name'],
                'is_group'=>$data['is_group'],
                'is_write'=>$data['is_write'],
                'update_time'=>time(),
            ])){
                throw new Exception('修改失败，请联系管理员');
            }

            if ($userId != $employeeInfo['user_id']){

                $StoreInfoModel->insertGetId([
                    'store_id' => $storeId,
                    'merchant_id' => $user->merchant_id,
                    'user_id' => $userInfo['id'],
                    'is_manager' => StoreInfo::NOT_MANAGER,
                    'create_time' => time(),
                ]);

                $userInfo->is_employee = User::IS_EMPLOYEE_YES;
                $userInfo->save();

                if ($oriUserInfo = User::where('id',$employeeInfo['user_id'])->find()){
                    $StoreInfoModel->where('user_id',$employeeInfo['user_id'])->where('store_id',$employeeInfo['store_id'])->delete();
                    $oriUserInfo->is_employee = User::IS_EMPLOYEE_NO;
                    $oriUserInfo->save();
                }
            }

            if ($data['is_assistant'] && $data['level']) {
                $levelInfo = Level::where('vl_id', $data['level'])->find();
                if (!$levelInfo) {
                    throw new Exception('当前等级不存在，请联系管理员');
                }
                $assistantModel = new Assistant();
                if ($employeeInfo['user_id'] != $userId){
                    if (!$newAssistant = Assistant::where('user_id',$userId)->find()){
                        if (!$assistantId = $assistantModel->insertGetId([
                            'user_id' => $userId,
                            'type' => Assistant::TYPE_STORE,
                            'store_id' => $storeId,
                            'employee_id' => $id,
                            'nickname' => $userInfo['nickname'] ?? '',
                            'gender' => $userInfo['gender'] ?? '',
                            'avatar' => $userInfo['avatar'] ?? '',
                            'birthday' => is_null($userInfo['birthday']) ? '' : $userInfo['birthday'],
                            'level' => $data['level'],
                            'assistant_price' => $levelInfo['assistant_price'],
                            'time_unit' => $levelInfo['time_unit'],
                            'lat'=>$storeInfo['lat'],
                            'lng'=>$storeInfo['long'],
                            'address' => $storeInfo['address'],
                            'review'=>0,
                            'reason'=>'门店添加，无需审核',
                            'review_status'=>Assistant::REVIEW_STATUS_PASS,
                            'create_time' => time(),
                            'update_time' => time()

                        ])) {
                            throw new Exception('添加助教信息失败，请联系管理员');
                        }

                        $result = AssistantAccountManager::account($assistantId,$userInfo['id'],false);
                        if ($result['code'] != AssistantAccountManager::$successCode){
                            throw new Exception($result['message']);
                        }
                    }else{
                        $newAssistant->store_id = $storeId;
                        $newAssistant->type = Assistant::TYPE_STORE;
                        $newAssistant->employee_id = $id;
                        $newAssistant->save();
                    }


                    $oriAssistantInfo = $assistantModel->where('user_id',$employeeInfo['user_id'])->where('store_id',$storeId)->where('employee_id',$id)->find();
                    if ($oriAssistantInfo){
                        $oriAssistantInfo->store_id = 0;
                        $oriAssistantInfo->type = Assistant::TYPE_PERSONAL;
                        $oriAssistantInfo->employee_id = 0;
                        $oriAssistantInfo->save();
                    }
                }else{
                    $oriAssistantInfo = $assistantModel->where('user_id',$employeeInfo['user_id'])->where('store_id',$storeId)->where('employee_id',$id)->find();
                    if ($oriAssistantInfo){
                        $oriAssistantInfo->level = $data['level'];
                        $oriAssistantInfo->assistant_price = $levelInfo['assistant_price'];
                        $oriAssistantInfo->time_unit = $levelInfo['time_unit'];
                        $oriAssistantInfo->save();
                    }else{
                        if (!$assistantId = $assistantModel->insertGetId([
                            'user_id' => $employeeInfo['user_id'],
                            'type' => Assistant::TYPE_STORE,
                            'store_id' => $storeId,
                            'employee_id' => $id,
                            'nickname' => $userInfo['nickname'] ?? '',
                            'gender' => $userInfo['gender'] ?? '',
                            'avatar' => $userInfo['avatar'] ?? '',
                            'birthday' => is_null($userInfo['birthday']) ? '' : $userInfo['birthday'],
                            'lat'=>$data['lat'],
                            'lng'=>$data['lng'],
                            'address'=>$data['address'],
                            'level' => $data['level'],
                            'assistant_price' => $levelInfo['assistant_price'],
                            'time_unit' => $levelInfo['time_unit'],
                            'review'=>0,
                            'reason'=>'门店添加，无需审核',
                            'review_status'=>Assistant::REVIEW_STATUS_PASS,
                            'create_time' => time(),
                            'update_time' => time()

                        ])) {
                            throw new Exception('添加助教信息失败，请联系管理员');
                        }

                        $result = AssistantAccountManager::account($assistantId,$userInfo['id'],false);
                        if ($result['code'] != AssistantAccountManager::$successCode){
                            throw new Exception($result['message']);
                        }
                    }
                }



            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }


    /**
     * 系统修改店员信息
     * @param int $id
     * @param array $data
     * @param int $storeId
     * @param bool $trans
     * @return array
     */
    public static function modifySys(int $id,array $data,int $storeId,bool $trans=true):array
    {
        if ($trans){
            Db::startTrans();
        }
        try {

            if (!$employeeInfo = StoreEmployee::where('id', $id)->where('delete',StoreEmployee::DELETE_NO)->find()){
                throw new Exception('当前记录不存在，无法修改');
            }

            $storeEmployeeModel = new StoreEmployee();
            $StoreInfoModel = new StoreInfo();

            if ($storeEmployeeModel->where('mobile',$data['mobile'])->where('store_id',$storeId)->where('id','<>',$id)->find()){
                throw new Exception('当前员工已存在，无法修改');
            }

            $userId = 0;
            $userInfo = User::where('mobile',$data['mobile'])->find();
            if ($userInfo){
                $userId = $userInfo['id'];
            }

            if(!$id = $storeEmployeeModel->where('id',$id)->where('delete',StoreEmployee::DELETE_NO)->update([
                'user_id'=>$userId,
                'mobile'=>$data['mobile'],
                'real_name'=>$data['real_name'],
                'is_group'=>$data['is_group'],
                'is_write'=>$data['is_write'],
                'update_time'=>time(),
            ])){
                throw new Exception('修改失败，请联系管理员');
            }
            $storeInfo = Store::where('id',$storeId)->find();
            if (!$storeInfo){
                throw new Exception('当前门店信息不不能在');
            }
            if ($userId && $employeeInfo['user_id'] &&  $userId != $employeeInfo['user_id']){
                if ($nowUserInfo = User::where('id', $userId)->find()){

                    $StoreInfoModel->insertGetId([
                        'store_id' => $storeId,
                        'merchant_id' => $storeInfo->merchant_id,
                        'user_id' => $userInfo['id'],
                        'is_manager' => StoreInfo::NOT_MANAGER,
                        'create_time' => time(),
                    ]);

                    $nowUserInfo->is_employee = User::IS_EMPLOYEE_YES;
                    $nowUserInfo->save();
                }

                if ($oriUserInfo = User::where('id',$employeeInfo['user_id'])->find()){
                    $StoreInfoModel->where('user_id',$employeeInfo['user_id'])->where('store_id',$employeeInfo['store_id'])->delete();
                    $oriUserInfo->is_employee = User::IS_EMPLOYEE_NO;
                    $oriUserInfo->save();
                }
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }

    /**
     * 商户管理删除店员
     * @param User $user
     * @param int $id
     * @param bool $trans
     * @return array
     */
    public static function destroy(User $user,int $id,bool $trans=false)
    {
        if ($trans){
            Db::startTrans();
        }
        try {

            if (!$employeeInfo = StoreEmployee::where('id', $id)->where('delete',StoreEmployee::DELETE_NO)->find()){
                throw new Exception('当前记录不存在，无法删除');
            }

            $storeEmployeeModel = new StoreEmployee();
            $StoreInfoModel = new StoreInfo();
            $storeInfo = $StoreInfoModel->where('merchant_id', $user->merchant_id)->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find();
            if (!$storeInfo){
                throw new Exception('您当前无操作权限');
            }

            if (!empty($storeInfo['store_id']) && $storeInfo['store_id'] != $employeeInfo['store_id']){
                if (!$StoreInfoModel->where('merchant_id',$user->merchant_id)->where('store_id',$employeeInfo['store_id'])->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find()){
                    throw new Exception('您非当前门店管理人员，无操作权限');
                }
            }


            if(!$storeEmployeeModel->where('id',$id)->where('delete',StoreEmployee::DELETE_NO)->delete()){
                throw new Exception('删除失败，请联系管理员');
            }

            if ($employeeInfo['user_id']){
                if ($nowUserInfo = User::where('id', $employeeInfo['user_id'])->find()){
                    $nowUserInfo->is_employee = User::IS_EMPLOYEE_NO;
                    $nowUserInfo->save();

                    $StoreInfoModel->where('user_id',$employeeInfo['user_id'])->where('store_id',$employeeInfo['store_id'])->delete();
                }
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }


    /**
     * 系统删除店员
     * @param int $id
     * @param bool $trans
     * @return array
     */
    public static function destroySys(int $id,bool $trans=false)
    {
        if ($trans){
            Db::startTrans();
        }
        try {

            if (!$employeeInfo = StoreEmployee::where('id', $id)->find()){
                throw new Exception('当前记录不存在，无法删除');
            }

            $storeEmployeeModel = new StoreEmployee();
            $StoreInfoModel = new StoreInfo();


            if(!$storeEmployeeModel->where('id',$id)->delete()){
                throw new Exception('删除失败，请联系管理员');
            }

            if ($employeeInfo['user_id']){
                if ($nowUserInfo = User::where('id', $employeeInfo['user_id'])->find()){
                    $nowUserInfo->is_employee = User::IS_EMPLOYEE_NO;
                    $nowUserInfo->save();

                    $StoreInfoModel->where('user_id',$employeeInfo['user_id'])->where('store_id',$employeeInfo['store_id'])->delete();
                }
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }

    /**
     * @param User $user
     * @param int $id
     * @param int $page
     * @param int $step
     * @return void
     */
    public static function list(User $user,int $id,int $page=1,int $step=10):array
    {
        $return = ['total'=>0,'totalPage'=>1,'currentPage'=>$page,'list'=>[]];
        if ($page < 1){
            $page = 1;
        }
        try {
            $StoreInfoModel = new StoreInfo();
            $storeInfo = $StoreInfoModel->where('merchant_id', $user->merchant_id)->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find();
            if (!$storeInfo){
                throw new Exception('您当前无操作权限');
            }
            if (!empty($storeInfo['store_id']) && $storeInfo['store_id'] != $id){
                if (!$StoreInfoModel->where('merchant_id',$user->merchant_id)->where('store_id',$id)->where('user_id',$user->id)->where('is_manager',StoreInfo::IS_MANAGER)->find()){
                    throw new Exception('您非当前门店管理人员，无操作权限');
                }
            }
            $where['se.delete'] = StoreEmployee::DELETE_NO;
            $where['se.store_id'] = $id;
            $start = ($page-1)*$step;
            $storeEmployeeModel = new StoreEmployee();
            $count = $storeEmployeeModel->alias('se')
                ->join('user u','se.user_id=u.id','left')
                ->join('assistant a','a.employee_id=se.id','left')
                ->where($where)
                ->count();

            $info = $storeEmployeeModel->alias('se')
                ->join('user u','se.user_id=u.id','left')
                ->join('assistant a','a.employee_id=se.id','left')
                ->where($where)
                ->field('a.id as assistant_id,a.lat,a.lng,a.address,a.level,se.id,se.merchant_id,se.store_id,se.user_id,se.mobile as semobile,se.real_name as sereal_name,se.is_group,se.is_write,se.create_time,u.nickname,u.avatar')
                ->order('se.id desc')
                ->limit($start,$step)
                ->select();

            if($info){
                foreach ($info as $k=>&$v){
                    if ($v->avatar && strrpos($v->avatar,'http')===false){
                        $v->avatar = cdnurl($v->avatar);
                    }
                }
            }
            $pageData = page($count,$step,$page);
            $return['list'] = $info;
            $return = array_merge($return,$pageData);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($return);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData() ];
    }

    public static function authCheck($userId,$storeId,$is_store,\app\common\model\Team $team=null,$type=self::TYPE_GROUP)
    {
        try {
            if ($is_store == \app\common\model\Team::IS_STORE_YES){
                $storeInfo = Store::where('id',$storeId)->find();
                if (!$storeInfo){
                    throw new Exception('当前门店信息不存在，无法操作');
                }

                $storeEmployeeInfo = null;
                $storeInfoData = StoreInfo::where('merchant_id',$storeInfo['merchant_id'])->where('user_id',$userId)->where('is_manager',StoreInfo::IS_MANAGER)->find();
                if (!$storeInfoData){
                    $storeEmployeeInfo = StoreEmployee::where('store_id',$storeId)->where('user_id',$userId)->find();
                    if (!$storeEmployeeInfo){
                        throw new Exception('您非员工无权限操作');
                    }
                }

                if (!$storeInfoData && $storeEmployeeInfo){
                    switch ($type){
                        case self::TYPE_GROUP:
                            if ($storeEmployeeInfo['is_group'] != StoreEmployee::IS_GROUP_YES){
                                throw new Exception('您无权限组队操作');
                            }
                            break;
                        case self::TYPE_WRITE:
                            if ($storeEmployeeInfo['is_write'] != StoreEmployee::IS_WRITE_YES){
                                throw new Exception('您无权限核销操作');
                            }
                            break;
                        default:
                            break;
                    }
                }


            }else{

                if ($team['captain'] != $userId){
                    throw new Exception('当前用户无权操作');
                }
            }
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }

}